package com.ag.answergames.service.answerlog.impl;

import com.ag.answergames.entity.AnswerLogEntity;
import com.ag.answergames.mapper.answerlog.AnswerLogMapper;
import com.ag.answergames.service.answerlog.AnswerLogService;
import com.ag.answergames.util.MyTimeUtil;
import com.ag.answergames.util.SnowflakeIdWorkerController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AnswerLogServiceImpl implements AnswerLogService {

    @Autowired
    SnowflakeIdWorkerController snowId;

    @Autowired
    private AnswerLogMapper answerLogMapper;

    @Override
    public Long getTotalCount(String openId, int dateType) {
        QueryWrapper<AnswerLogEntity> wrapper = Wrappers.query();
        wrapper = wrapper.eq("student_id", openId);
        dateTypeChange(wrapper, dateType);
        return answerLogMapper.selectCount(wrapper);
    }

    @Override
    public Long getCorrectCount(String openId, int dateType) {
        QueryWrapper<AnswerLogEntity> wrapper = Wrappers.query();
        wrapper = wrapper.eq("student_id", openId).eq("if_correct",1);
        dateTypeChange(wrapper, dateType);
        return answerLogMapper.selectCount(wrapper);
    }

    @Override
    public int setLog(AnswerLogEntity answerLogEntity) {
        answerLogEntity.setId(snowId.nextId());
        return answerLogMapper.insert(answerLogEntity);
    }

    @Override
    public int setLog(String OpenId,Long QuestionId,String Choose,boolean ifCorrect) {
        AnswerLogEntity answerLogEntity=new AnswerLogEntity();
        return 1;
    }

    private <T> QueryWrapper<T> dateTypeChange(QueryWrapper<T> wrapper, int dateType) {
        switch (dateType) {
            case 1:
                wrapper.gt("create_time", MyTimeUtil.getDayBegin());
                break;
            case 2:
                wrapper.gt("create_time", MyTimeUtil.getWeekBegin());
                break;
            case 3:
                wrapper.gt("create_time", MyTimeUtil.getMonthBegin());
                break;
            case 4:
                break;
            default:
                break;
        }
        return wrapper;
    }
}
